package com.cjw.campus.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjw.campus.system.entity.TeacherTable;
import com.cjw.campus.system.service.ITeacherTableService;
import com.cjw.campus.system.vo.TeacherTableVO;
import core.result.JsonResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 教师表 前端控制器
 * </p>
 *
 * @author cjw
 * @since 2022-12-04
 */
@RestController
@RequestMapping("/system/teacherTable")
@Api(tags = "教师管理", value = "教师管理接口")
public class TeacherTableController {

    @Autowired
    private ITeacherTableService teacherTableService;

    @GetMapping("/listPageByTeacherName")
    @ApiOperation(value = "模糊分页查询教师信息", notes = "模糊分页查询教师信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", defaultValue = "2"),
            @ApiImplicitParam(name = "teacherName", value = "教师姓名", dataType = "String")
    })
    public JsonResult listPageByTeacherName (Integer currentPage,Integer pageSize,String teacherName) {
        IPage<TeacherTableVO> page = new Page<>(currentPage, pageSize);
        IPage<TeacherTableVO> mapPage = teacherTableService.listPageByTeacherName(page, teacherName);
        return JsonResult.ok(mapPage);
    }

    @GetMapping("/listPageByTeacherId")
    @ApiOperation(value = "根据id查询教师信息", notes = "根据id查询教师信息", httpMethod = "GET")
    @ApiParam(name = "teacherId", value = "教师ID", required = true)
    public JsonResult listPageByTeacherId (Long teacherId) {
        TeacherTableVO listPageByTeacherId = teacherTableService.listPageByTeacherId(teacherId);
        return JsonResult.ok(listPageByTeacherId);
    }

    @PostMapping("/Save")
    @ApiOperation(value = "保存教师信息", notes = "保存教师信息", httpMethod = "POST")
    @ApiParam(name = "teacherTable", value = "教师实体类", required = true)
    public JsonResult save (TeacherTable teacherTable) {
        // 保存年级信息
        boolean save = teacherTableService.save(teacherTable);
        // 判断年级是否新增成功
        if (save) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("新增失败");
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id更新教师信息", notes = "根据id更新教师信息", httpMethod = "POST")
    @ApiParam(name = "teacherTable", value = "教师实体类", required = true)
    public JsonResult updateById (TeacherTable teacherTable) {
        boolean updateById = teacherTableService.updateById(teacherTable);
        // 判断用户是否修改成功
        if (updateById) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("修改失败");
    }

    @GetMapping("/removeById")
    @ApiOperation(value = "根据id删除教师信息", notes = "根据id删除教师信息", httpMethod = "GET")
    @ApiParam(name = "teacherId", value = "教师ID", required = true)
    public JsonResult removeById (Long teacherId) {
        boolean removeId = teacherTableService.removeById(teacherId);
//         判断用户是否删除成功
        if (removeId) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

    @PostMapping ("/removeByIds")
    @ApiOperation(value = "根据id批量删除教师信息", notes = "根据id批量删除教师信息", httpMethod = "POST")
    @ApiParam(name = "teacherId", value = "教师ID", required = true)
    public JsonResult removeByIds (Long[] idList) {
        boolean removeIds = teacherTableService.removeByIds(Arrays.asList(idList));
        if (removeIds) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询所有教师信息", notes = "查询所有教师信息", httpMethod = "GET")
    public JsonResult list () {
        List<TeacherTable> list = teacherTableService.list();
        return JsonResult.ok(list);
    }

}
